import json

from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect


# Create your views here.
#正则访问参数 确认键值对应的信息
from django.urls import reverse


def hello(request,age,name):
    print(age,name)
    return HttpResponse("hello world")

#get访问
def get_view(request):
    name=request.GET.get("name")
    age=request.GET.get("age")
    names=request.GET.getlist("name")
    return HttpResponse("name%s,age%s,names%s"%(name,age,names))


def post_view(request):
    if request.method=="POST":
        name=request.POST.get("name")
        age=request.POST.get("age")
        names=request.POST.getlist("name")
        print(names)
        return HttpResponse("post请求方式,name%s,age%s" % (name, age))

#put方法等同于post方法 delete等同于get方法

#其他属性
def get_cookie(request):
    cookie=request.COOKIES
    user=request.user
    method=request.method
    print(cookie)
    print(user)
    print(method)
    return HttpResponse("cookie")


def json_rep(request):
    di={"name":"hanlei","age":29}
    li=[{"name":"hanlei","age":29}]
    return JsonResponse(li,safe=False)  #加入safe设置false就可以传输列表信息的json数据

def redirect_view(request):
    # url=reverse("app01:jsondata")
    # return redirect(url)
    return redirect('/app1/json/')


def set_cookie(request):
    response=HttpResponse("设置cookie")
    response.set_cookie("name","hanlei",max_age=20)
    #设置cookie时间20秒，可以在前端的调试页面中的cookies中查看20秒是否还在
    return response

def get_cookie_view(request):
    name=request.COOKIES.get("name") #如果cookie时间过期就会返回为None
    return HttpResponse(name)


def set_session(request):
    request.session['name']="hanlei"
    request.session.set_expiry(600)

    return HttpResponse("set session")

def del_session(request):
    # request.session.pop("name")
    # del request.session["name"]
    # request.session.clear()
    # request.session.flush()
    return HttpResponse("del session")


def get_session(request):
    name=request.session.get("name")
    return HttpResponse(name)


